package UILayer;
import CtrLayer.ContractorCtr;


/**
 * Write a description of class ContractorUI here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ContractorUI extends InOut
{
    private static ContractorCtr constCtr = new ContractorCtr();
    private static int idConst = 1000;

    /**
     * Constructor for objects of class ContractorUI
     */
    public ContractorUI()
    {
        
    }
    
    public void contractorMenu()
    {
        String[] contractorMenu = {"Contractor MENU", "Create contractor", "Retrieve contractor's information", "Update contractor's information", "Delete contractor"};
        printMenu(contractorMenu);
    }
    
    public void start()
    {
        boolean exit = false;
        while(!exit)
        {
            erase(1);
            contractorMenu();
            int choice = inputInt("Choose: ");
            erase(1);
            switch(choice)
            {
                case 1 : {createContractor();}break;
                case 2 : {getContractor();}break;
                case 3 : {updateContractor();}break;
                case 4 : {deleteContractor();}break;
                case 5 : {exit=true;}break;

                default : {}break;
            }
            if(choice>=1 && choice<=4 && choice!=3) waitK();
        }
    }
    
    public void createContractor()
    {
        idConst++;
        String n = inputString("Enter contractor's name: ");
        String a = inputString("Enter contractor's address: ");
        String pc = inputString("Enter contractor's postal code: ");
        String ph = inputString("Enter contractor's phone number: ");
        String e = inputString("Enter contractor's e-mail address: ");
        String desc = inputString("Enter contractor's description: ");
        constCtr.createContractor(idConst, n, a, pc, ph, e, desc);
        System.out.println("Contractor with ID: " +idConst+" created.");
    }
    
    public void getContractor()
    {
        int id = inputInt("Enter contractor's ID: ");
        try {
            print("######## CONTRACTOR INFORMATION ########");
            print("ID: " + constCtr.getContractor(id).getId());
            print("Name: " + constCtr.getContractor(id).getName());
            print("Address: " + constCtr.getContractor(id).getAddress());
            print("Postal Code: " + constCtr.getContractor(id).getPostCode());
            print("Phone number: " + constCtr.getContractor(id).getPhone());
            print("E-mail address: " + constCtr.getContractor(id).getEmail());
            print("Description: " + constCtr.getContractor(id).getDescription());
        }
        catch(Exception e) { print("There is no customer with given id"); }
    }
    
    public void updateContractorMenu(int id)
    {
        String[] updateContMenu = {"Update Contractor with ID "+id, "Update name", "Update address", "Update postal code", "Update phone number", "Update email", "Update description"};
        printMenu(updateContMenu);
    }
    
    public void updateContractor()
    {
        int id = inputInt("Enter Contractor's ID: ");
        if(id==-1) print("-> Invalid contractor's ID.");
        else{
            boolean exit = false;
            while(!exit){
                erase(1);
                updateContractorMenu(id);
                int choice = inputInt("Choose: ");
                
                if(choice<1 || choice > 6) {writeEnd(); exit = true;}
                else{
                    String upInf = "New value: ";
                    updateContractor(id, choice, upInf);
                    waitK();
                }
            }
        }
    }
    
    public void updateContractor(int id, int opt, String upInf)
    {
        String newValue = inputString(upInf);
        if(constCtr.updateContractor(id, newValue, opt)) print(" -> The contractor was successfully updated.");
        else print(" -> Contactor with ID " +id+ " does not exist.");
    }
    
    public void deleteContractor()
    {
        int id = inputInt("Enter contractor's ID: ");
        if(constCtr.deleteContractor(id)) print(" -> The customer was successfully deleted.");
        else print(" -> Contractor with ID " +id+ " does not exist.");
    }
}
